package com.doujiao.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiao.auth.mapper.AdminGroupPermissionMapper;
import com.doujiao.auth.service.AdminGroupPermissionService;
import com.doujiao.auth.service.AdminGroupService;
import com.doujiao.core.auth.dto.AdminGroupPermissionCreateDTO;
import com.doujiao.core.auth.dto.AdminGroupPermissionQueryDTO;
import com.doujiao.core.auth.entity.AdminGroup;
import com.doujiao.core.auth.entity.AdminGroupPermission;
import com.doujiao.core.auth.vo.AdminGroupPermissionPageVO;
import com.doujiao.core.exceptions.BusinessException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管理员组权限服务实现类
 *
 * @author wuyunbin
 * @since 2025-05-24
 */
@Service
@RequiredArgsConstructor
public class AdminGroupPermissionServiceImpl extends ServiceImpl<AdminGroupPermissionMapper, AdminGroupPermission> implements AdminGroupPermissionService {

    private final AdminGroupService adminGroupService;

    @Override
    public AdminGroupPermissionPageVO page(AdminGroupPermissionQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<AdminGroupPermission> wrapper = new LambdaQueryWrapper<>();
        if (queryDTO.getGroupId() != null) {
            wrapper.eq(AdminGroupPermission::getGroupId, queryDTO.getGroupId());
        }
        if (StringUtils.isNotBlank(queryDTO.getPermission())) {
            wrapper.like(AdminGroupPermission::getPermission, queryDTO.getPermission());
        }
        wrapper.orderByDesc(AdminGroupPermission::getCreateTime);

        // 执行分页查询
        Page<AdminGroupPermission> page = new Page<>(queryDTO.getCurrent(), queryDTO.getSize());
        page = this.page(page, wrapper);

        // 封装返回结果
        AdminGroupPermissionPageVO pageVO = new AdminGroupPermissionPageVO();
        pageVO.setTotal(page.getTotal());
        pageVO.setCurrent(page.getCurrent());
        pageVO.setSize(page.getSize());
        pageVO.setRecords(page.getRecords());

        return pageVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<AdminGroupPermission> batchCreate(AdminGroupPermissionCreateDTO createDTO) {
        // 检查管理员组是否存在
        AdminGroup adminGroup = adminGroupService.getById(createDTO.getGroupId());
        if (adminGroup == null) {
            throw new BusinessException("管理员组不存在");
        }

        // 删除原有权限
        this.deleteByGroupId(createDTO.getGroupId());

        // 批量创建新权限
        List<AdminGroupPermission> permissions = new ArrayList<>();
        for (String permission : createDTO.getPermissions()) {
            AdminGroupPermission adminGroupPermission = new AdminGroupPermission();
            adminGroupPermission.setGroupId(createDTO.getGroupId());
            adminGroupPermission.setPermission(permission);
            permissions.add(adminGroupPermission);
        }

        // 批量保存
        this.saveBatch(permissions);
        return permissions;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        // 检查权限是否存在
        AdminGroupPermission permission = this.getById(id);
        if (permission == null) {
            throw new BusinessException("权限不存在");
        }

        return this.removeById(id);
    }

    @Override
    public List<String> getPermissionsByGroupId(Long groupId) {
        LambdaQueryWrapper<AdminGroupPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminGroupPermission::getGroupId, groupId);

        return this.list(wrapper).stream()
                .map(AdminGroupPermission::getPermission)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByGroupId(Long groupId) {
        LambdaQueryWrapper<AdminGroupPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminGroupPermission::getGroupId, groupId);
        return this.remove(wrapper);
    }
}
